import java.util.Date;
/**	
 * This class ...
 */
public class Bus implements Comparable<Bus>
{
  public int busId;
  public String busNumber;
  public int[] startMin;
  public int[] endMin;
  public int[] service;
  private static int array_index = 30;
  public String busTimetable;
  public int timeUsed;
  //private String assignedBus;
  
  public Bus(int id, String string)
  {
    this.busId=id;
    this.busNumber=string;
    startMin = new int[array_index];
    endMin = new int[array_index];
    service = new int[array_index];
    busTimetable = new String();
    timeUsed = 0;
  }//Bus#
  //assume that driverList will be ordered each time according to availability
  public static void assignBusToDriver(int start, int end, Driver driver, int theService, Date theDate, Bus[] busList)
  {
	 //if the end is after 24*60min
     if(end<start && start>1340) end+=1440;
     for(int i =0;i<busList.length;i++)
     {
 	boolean found=true;
	//CONDITION 1 --check if driver is available
	if(!BusInfo.isAvailable(busList[i].busId,theDate))
	  found= false; 
	//CONDITION 2 --slot is free
    for(int index=0;index<array_index&&found;index++)
	{
  	  if(between(busList[i].startMin[index],busList[i].endMin[index],start) ||
             between(busList[i].startMin[index],busList[i].endMin[index],end)   ||
             (start<=busList[i].startMin[index] && end>=busList[i].endMin[index]))
              found=false;
	}
	if(found)
    {  busList[i].timeUsed+=Math.abs(end-start);
       driver.assignedBus = busList[i].busNumber;
	    //update bus to be busy between start and end and update the minutes in the day
        for(int index =0; index<array_index;index++)
	    {
            if(busList[i].startMin[index]==busList[i].endMin[index]&&
               busList[i].startMin[index]==0)
            {
               busList[i].startMin[index]=start;
	           busList[i].endMin[index]=end; 
	           busList[i].service[index]=theService;
	           return; //  return driversList[i];  
            }
	    }
     } //if found
	
     }//for 0 -busList.length
     System.out.println("8********************************************************8");
   }
   
   private static boolean between(int start, int end, int x)
   { 
     if(x>=start&&x<=end)return true;
     else return false;
   }
   
   public void updateBusTimeTable(Date theDate)
   {
//       String dailyTable = new String();
//       if(service[0]!=0)
//       {
//         dailyTable=this.busNumber+"\n";
//       }
//       for(int i=0;i<array_index;i++)
//	       dailyTable +=" [Service: "+ service[i]+"] \n";
//           busTimetable+=dailyTable;
//       //reset all the information for the driver in one day;
       this.startMin = new int[array_index];
       this.endMin = new int[array_index];
       this.service = new int[array_index];

   }//updateBus
@Override
public int compareTo(Bus x)
{
	if(this.timeUsed<x.timeUsed)return -1;
	if(this.timeUsed>x.timeUsed)return 1;
	return 0;
}//compareTo
@Override
public boolean equals( Object x )
{
     if ( this == x ) return true;
     if ( !(x instanceof Bus) ) return false; 
     else return true;
}//equals
   
}//class Bus
